package com.xxxt.wsc.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.xxxt.dao.CompanyMapper;
import com.xxxt.dao.PracticeMapper;
import com.xxxt.dao.StudentMapper;
import com.xxxt.entity.Company;
import com.xxxt.entity.Practice;
import com.xxxt.entity.Student;
import com.xxxt.utils.FileUploadUtil;
import com.xxxt.utils.PracticeTestifyUtil;
import com.xxxt.utils.UUIDUtil;
import com.xxxt.wsc.service.PracticeService;

/**
 * @description:
 * @author:wsc
 * @date:2020年6月11日 上午9:31:38
 */
@Service
@Transactional
public class PracticeServiceImpl implements PracticeService{

	static final String ip = "https://jiaojiaoni.com";
	@Autowired
	PracticeMapper practiceMapper;
	
	@Autowired
	StudentMapper studentMapper;
	
	@Autowired
	CompanyMapper companyMapper;
	
	@Override
	public List<Practice> queryPracticeListBySno(String sno) {
		//获取实习记录列表
		List<Practice> list = practiceMapper.queryPracticeListBySno(sno,null,null);
		return list;
	}

	@Override
	public Map<String,Object> queryPracticeInfoById(String practiceId) {
		Map<String,Object> map = new HashMap<String,Object>();
		List<String> entryTestify = new ArrayList<String>();
		List<String> leaveTestify = new ArrayList<String>();
		//获取实习记录对象
		Practice practice = practiceMapper.queryPracticeById(practiceId);
		//获取学生对象
		Student student = studentMapper.queryOneStudentSchoolInfoBySno(practice.getStudent().getSno());
		//拼接服务器路径
		if(practice.getEntryTestify()!=null && !"".equals(practice.getEntryTestify())) {
			String[] testify = practice.getEntryTestify().split(",");
			for(String s:testify) {
				entryTestify.add(ip+ "/" +PracticeTestifyUtil.getTemporaryPath(student)+s);
			}
			practice.setEntryTestify(null);
		}
		if(practice.getLeaveTestify()!=null && !"".equals(practice.getLeaveTestify())) {
			String[] testify = practice.getLeaveTestify().split(",");
			for(String s:testify) {
				leaveTestify.add(ip+ "/" +PracticeTestifyUtil.getTemporaryPath(student)+s);
			}
			practice.setLeaveTestify(null);
		}
		map.put("practice", practice);
		map.put("entryTestify", entryTestify);
		map.put("leaveTestify", leaveTestify);
		return map;
	}

	//TODO
	@Override
	public boolean updateEntryPractice(
			String practiceId,String entryTime,String job,
			String leaderName,String leaderPhone,
			List<String> entryTestify) {
		Practice practice = practiceMapper.queryPracticeById(practiceId);
		practice.setEntryTime(entryTime);
		practice.setJob(job);
		practice.setLeaderName(leaderName);
		practice.setLeaderPhone(leaderPhone);
		practice.setEntryTestify("");
		for(String entry:entryTestify) {
			entry = new File(entry).getName();
			practice.setEntryTestify(practice.getEntryTestify()+entry+",");
		}
		int i = practiceMapper.updatePractice(practice);
		if(i>=1) {
			return true;
		}
		return false;
	}

	//TODO
	@Override
	public boolean addPractice(String entryTime,String companyId,String sno, String job, 
			String leaderName, String leaderPhone,
			List<String> entryTestify) {
		Practice practice = new Practice();
		practice.setPracticeId(UUIDUtil.getUUID());
		practice.setEntryTime(entryTime);
		practice.setCompany(companyMapper.queryCompanyByCompanyId(companyId));
		practice.setStudent(studentMapper.queryOneStudentBySno(sno));
		practice.setJob(job);
		practice.setLeaderName(leaderName);
		practice.setLeaderPhone(leaderPhone);
		practice.setEntryTestify("");
		for(String entry:entryTestify) {
			entry = new File(entry).getName();
			practice.setEntryTestify(practice.getEntryTestify()+entry+",");
		}
		//修改学生对应的实习公司id
		Student student = new Student();
		Company company = new Company();
		company.setCompanyId(companyId);
		student.setSno(sno);
		student.setCompany(company);
		studentMapper.updateStudentCompanyBySno(student);
		int i = practiceMapper.addPractice(practice);
		if(i>=1) {
			return true;
		}
		return false;
	}

	@Override
	public boolean deletePractice(HttpServletRequest request,String practiceId) throws IOException {
		Practice practice = practiceMapper.queryPracticeById(practiceId);
		//获取学生对象
		Student student = studentMapper.queryOneStudentSchoolInfoBySno(practice.getStudent().getSno());
		String temporary = PracticeTestifyUtil.getTemporaryPath(student);
		
		String entryTestify = practice.getEntryTestify();
		String leaveTestify = practice.getLeaveTestify();
		
		if(entryTestify!=null && "".equals(entryTestify)) {
			String[] split = entryTestify.split(",");
			for(String s:split) {
				//删除实习记录证明材料
				PracticeTestifyUtil.deleteAllDirectorysFile(request, temporary+s);
			}
		}
		if(leaveTestify!=null && "".equals(leaveTestify)) {
			String[] split = leaveTestify.split(",");
			for(String s:split) {
				//删除实习记录证明材料
				PracticeTestifyUtil.deleteAllDirectorysFile(request, temporary+s);
			}
		}
		//删除实习记录数据库数据
		int i = practiceMapper.deletePracticeById(practiceId);
		if(i>0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean updateLeavePractice(String practiceId, String leaveTime, String leaveReason,
			List<String> leaveTestify, HttpServletRequest request, HttpServletResponse response) {
		
		Practice practice = practiceMapper.queryPracticeById(practiceId);
		practice.setLeaveReason(leaveReason);
		practice.setLeaveTime(leaveTime);
		practice.setLeaveTestify("");
		for(String leave:leaveTestify) {
			leave = new File(leave).getName();
			practice.setLeaveTestify(practice.getLeaveTestify()+leave+",");
		}
		//修改学生对应的实习公司id
		Student student = new Student();
		Company company = new Company();
		student.setSno(practice.getStudent().getSno());
		student.setCompany(company);
		studentMapper.updateStudentCompanyBySno(student);
		int i = practiceMapper.updatePractice(practice);
		if(i>=1) {
			return true;
		}
		return false;
	}

	@Override
	public boolean isRecord(String sno) {
		List<Practice> list = practiceMapper.queryPracticeListBySno(sno,"is null",null);
		if(list.size()>0) {
			//获取最新的实习记录
			Practice practice = list.get(0);
			if(practice.getLeaveTime()==null) {
				return true;
			}else {
				return false;
			}
		}
		return false;
	}

	@Override
	public boolean isJob(String sno, String companyId) {
		List<Practice> list = practiceMapper.queryPracticeListBySno(sno, null, companyId);
		if(list.size()<1) {
			return false;
		}
		return true;
	}

	@Override
	public String uploadTestify(HttpServletRequest request,HttpServletResponse response,MultipartFile file,String sno) {
		//获取学生对象
		Student student = studentMapper.queryOneStudentSchoolInfoBySno(sno);
		//获取该生的临时文件夹路径
		String temporaryPath = PracticeTestifyUtil.getTemporaryPath(student);
		//判断文件夹是否存在
		boolean b = FileUploadUtil.isNotFile(request, temporaryPath);
		if(!b) {
			FileUploadUtil.createDirectory(request, response, temporaryPath);
		}
		File file2 = FileUploadUtil.upFilebySpring(file, request, response, UUIDUtil.getUUID(), temporaryPath);
		//获取上传文件路径
		String filePath = ip+ File.separator +temporaryPath+file2.getName();
		return filePath;
	}

	//删除根据文件的绝对路径临时文件
	@Override
	public void deleteTestify(String filePath) {
		PracticeTestifyUtil.deleteAbsolutelyTestify(filePath);
	}

	@Override
	public boolean addEntryOnPC(String entryTime, String companyId, String sno, String job, String leaderName,
			String leaderPhone, MultipartFile[] entryTestify, HttpServletRequest request,
			HttpServletResponse response) {
		Practice practice = new Practice();
		practice.setPracticeId(UUIDUtil.getUUID());
		practice.setEntryTime(entryTime);
		practice.setCompany(companyMapper.queryCompanyByCompanyId(companyId));
		practice.setStudent(studentMapper.queryOneStudentBySno(sno));
		practice.setJob(job);
		practice.setLeaderName(leaderName);
		practice.setLeaderPhone(leaderPhone);
		practice.setEntryTestify("");
		//获取存放图片目录路径
		String testifyPath = PracticeTestifyUtil.getTemporaryPath(studentMapper.queryOneStudentSchoolInfoBySno(sno));
		//判断目录存在与否
		if(!FileUploadUtil.isNotFile(request, testifyPath)) {
			FileUploadUtil.createDirectory(request, response, testifyPath);
		}
		for(MultipartFile entry:entryTestify) {
			File file = FileUploadUtil.upFilebySpring(entry, request, response, UUIDUtil.getUUID(), testifyPath);
			practice.setEntryTestify(practice.getEntryTestify()+file.getName()+",");
		}
		//修改学生对应的实习公司id
		Student student = new Student();
		Company company = new Company();
		company.setCompanyId(companyId);
		student.setSno(sno);
		student.setCompany(company);
		studentMapper.updateStudentCompanyBySno(student);
		int i = practiceMapper.addPractice(practice);
		if(i>=1) {
			return true;
		}
		return false;
	}

	@Override
	public boolean addLeaveOnPC(String practiceId, String leaveTime, String leaveReason, MultipartFile[] leaveTestify,
			HttpServletRequest request, HttpServletResponse response) {
		Practice practice = practiceMapper.queryPracticeById(practiceId);
		practice.setLeaveReason(leaveReason);
		practice.setLeaveTime(leaveTime);
		practice.setLeaveTestify("");
		//获取存放图片目录路径
		String testifyPath = PracticeTestifyUtil.getTemporaryPath(studentMapper.queryOneStudentSchoolInfoBySno(practice.getStudent().getSno()));
		//判断目录存在与否
		if(!FileUploadUtil.isNotFile(request, testifyPath)) {
			FileUploadUtil.createDirectory(request, response, testifyPath);
		}
		for(MultipartFile leave:leaveTestify) {
			File file = FileUploadUtil.upFilebySpring(leave, request, response, UUIDUtil.getUUID(), testifyPath);
			practice.setLeaveTestify(practice.getLeaveTestify()+file.getName()+",");
		}
		//修改学生对应的实习公司id
		Student student = new Student();
		Company company = new Company();
		student.setSno(practice.getStudent().getSno());
		student.setCompany(company);
		studentMapper.updateStudentCompanyBySno(student);
		int i = practiceMapper.updatePractice(practice);
		if(i>=1) {
			return true;
		}
		return false;
	}

	@Override
	public boolean isRecordLeave(String practiceId) {
		Practice practice = practiceMapper.queryPracticeById(practiceId);
		if(practice.getLeaveTime()!=null) {
			return true;
		}
		return false;
	}

}
